home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-04 | 2.9 KB | 120 lines | [TEXT/CWIE] |
- // Resources in the shared library world are tricky.
- // If you want to use any of the resources in your shared library then you need
- // to reset the current resource file to your shared library, then return it to the
- // initial condition when you finish. The easy way to do this is with a C++ object
- // which sets up your resource file on creation and restores it on destruction.
-
- #include "DllEntry.h"
-
- class CCFragResource
- {
- public:
- CCFragResource();
- ~CCFragResource();
-
- private:
- short InResRefNum;
- };
-
-
- CCFragResource::CCFragResource()
- {
- short ResRefNum = ::GetDllResRefNum();
- InResRefNum = ::CurResFile();
- if (ResRefNum != -1)
- ::UseResFile(ResRefNum);
- }
-
-
- CCFragResource::~CCFragResource()
- {
- if (InResRefNum != -1)
- ::UseResFile(InResRefNum);
- }
-
- // in your code, when you want to use resources
- {
- CFFragResource ResourceSetter;
- PicHandle PictH;
-
- PictH = GetResource('PICT',128);
-
- // ResourceSetter goes out of scope and restores the resource chain
- }
-
-
- // Getting the containing apps globals is done something like this:
-
-
- QDGlobalsPtr GetQDGlobals(void)
- {
- ProcessSerialNumber PSN;
- FSSpec myFSSpec;
- Str63 name;
- ProcessInfoRec infoRec;
- OSErr Result = noErr;
- CFragConnectionID connID;
- Str255 errName;
- QDGlobalsPtr QDGlobals = NULL;
- //
- // Ask the system if CFM is available.
- //
- long response;
- OSErr err = ::Gestalt(gestaltCFMAttr, &response);
- Boolean hasCFM = ::BitTst(&response, 31-gestaltCFMPresent);
-
- if (hasCFM)
- {
- //
- // GetProcessInformation takes a process serial number and
- // will give us back the name and FSSpec of the application.
- // See the Process Manager in IM.
- //
- infoRec.processInfoLength = sizeof(ProcessInfoRec);
- infoRec.processName = name;
- infoRec.processAppSpec = &myFSSpec;
-
- PSN.highLongOfPSN = 0;
- PSN.lowLongOfPSN = kCurrentProcess;
-
- Result = ::GetProcessInformation(&PSN, &infoRec);
- }
- else
- //
- // If no CFM installed, assume it must be a 68K app.
- //
- Result = -1;
-
- if (Result == noErr)
- {
- //
- // Now that we know the app name and FSSpec, we can call GetDiskFragment
- // to get a connID to use in a subsequent call to FindSymbol (it will also
- // return the address of “main” in app, which we ignore). If GetDiskFragment
- // returns an error, we assume the app must be 68K.
- //
- Ptr mainAddr;
- Result = ::GetDiskFragment(infoRec.processAppSpec, 0L, 0L, infoRec.processName,
- kLoadCFrag, &connID, (Ptr*)&mainAddr, errName);
- }
-
- if (Result == noErr)
- {
- //
- // The app is a PPC code fragment, so call FindSymbol
- // to get the exported “qd” symbol so we can access its
- // QuickDraw globals.
- //
- CFragSymbolClass SymClass;
- Result = ::FindSymbol(connID, "\pqd", (Ptr*)&QDGlobals, &SymClass);
- }
- else
- //
- // The app is 68K, so use its A5 to compute the address
- // of its QuickDraw globals.
- //
- QDGlobals = QDGlobalsPtr(*((long*)SetCurrentA5()) - (sizeof(QDGlobals) - sizeof(GrafPtr)));
-
- return QDGlobals;
- }
-